//
//  XGL_RequestBaseResponse.swift
//  Maternal
//
//  Created by ios on 2021/6/16.
//

import Foundation
import PanKit
import SmartCodable

struct Response: SmartCodable {
    
}

struct PageModel<T: SmartCodable>: SmartCodable{
    var currentPage: NSInteger = 0
    var pageSize: NSInteger = 0
    var totalPages: NSInteger = 0
    var totalElements: NSInteger = 0
    var data: [T] = []
}

class XGL_RequestBaseResponse<T: SmartCodable>: PanBaseReponseProtocol {
    
    typealias Response = T
    
    var resultData: T? = nil
    var responseResult: Bool?
    var resultPageDatas: [T]? = nil
    var resultDatas: [T]? = nil
    var resultCode: String = "200"
    var resultMessage: String = "Network error"
    var resultOther: String = ""
    var resultJson: String = ""
    var resultDic: [String:SmartAny<Any>]?
    var resultArray: [SmartAny<Any>]?
    var resultBool: Bool?
    var resultJsonData: Data?
    
    func result(arg: T) -> T? {
        return self.resultData
    }
    
    func result(arg: T) -> [T]? {
        return self.resultDatas
    }
    
    func code() -> String {
        guard let result = self.responseResult else { return self.resultCode }
        return result ? "0x200" : "0x10000"
    }
    
    func message() -> String {
        return self.resultMessage
    }
    
    func other() -> String {
        return self.resultOther
    }
    //MARK: 自定义映射规则
    static func mappingForKey() -> [SmartKeyTransformer]? {
        var keys: [SmartKeyTransformer] = [
            CodingKeys.resultData       <--- PanRequestConfig.share.responseReult,
            CodingKeys.responseResult   <--- PanRequestConfig.share.requestResult,
            CodingKeys.resultPageDatas  <--- "\(PanRequestConfig.share.responseReult).records",
            CodingKeys.resultDatas      <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultCode       <--- PanRequestConfig.share.responseCode,
            CodingKeys.resultMessage    <--- PanRequestConfig.share.responseMessage,
            CodingKeys.resultJson       <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultJsonData   <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultDic        <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultArray      <--- PanRequestConfig.share.responseReult,
            CodingKeys.resultBool       <--- PanRequestConfig.share.responseReult
        ]
        if let otherKey = PanRequestConfig.share.responseOther {
            keys.append(CodingKeys.resultOther  <--- otherKey)
        }
        return keys
    }
    
    required init() {
    }
}

